using System;
using System.Collections.Generic;
using CommonTypes.Math;
using CommonTypes.Math .Geometry3D ;
using CommonTypes.Events;

using System.Text;
using System.Collections;
using CommonTypes;


namespace BuildingBasicDomain
{
    [Serializable ]
    public class Polygon :List <IPoint>
    {
        public bool GeometryContains(IPoint point)
        {
            throw new NotImplementedException();
        }

        public bool GeometryContains(ILine line)
        {
            throw new NotImplementedException();
        }

        public bool IsConvex()
        {
            throw new NotImplementedException();
        }

        public bool IntersectBoundaries(IPoint point)
        {
            PolyLine<Line> polyline = (PolyLine<Line>)this;
            ILine line1 = new Line(this[0], point);
            ILine line2 = new Line(this[this.Count - 1], point);
            bool isIntersecting = false;

            isIntersecting =polyline.IsIntersecedBy (line1);
            isIntersecting =polyline.IsIntersecedBy (line2);

            return isIntersecting;
        }

        public double [,] BoundingCube(float scale)
        {
            double minX = this[0].X, minY = this[0].Y, maxX = this[0].X, maxY = this[0].Y,
                minZ = this[0].Z, maxZ = this[0].Z;
                
            double[,] rect = new double[2, 3];
            foreach (IPoint point in this)
            {
                if (point.X < minX)
                    minX = point.X;
                if ( point .Y < minY )
                    minY = point .Y ;
                if (point.X > maxX)
                    maxX = point.X;
                if (point.Y > maxY)
                    maxY = point.Y;
                if (point.Z < minZ)
                    minZ = point.Z;
                if (point.Z > maxZ)
                    maxZ = point.Z;
            }


            rect[0, 0] = minX * scale;
            rect[0, 1] = minY * scale;
            rect[0, 2] = minZ * scale;
            rect[1, 0] = maxX * scale;
            rect[1, 1] = maxY * scale;
            rect[1, 2] = maxZ * scale;
           
            return rect;
        }

        public IList<IPoint> LineIntersects(ILine _line)
        {
            PolyLine<Line> polyline = (PolyLine<Line>)this;
            return polyline.PointsIntersects(_line);
        }

        public double  Perimeter {
            get
            {
                PolyLine <Line> poly =  (PolyLine<Line>)this;
                double total =0 ;

                poly .ForEach (delegate (Line line)
                {
                    total = total + line.Length();
                   
                });
                return total;
            }
            
    }

        public static implicit operator ArrayList (Polygon polygon)
        {
            ArrayList list = new ArrayList();
            foreach (IPoint  node in polygon )
                list.Add(node );
            return list;
        }
        
    }
}
